<template>
	<view class="container">
		<view class="header">
			<text class="title">二十四节气</text>
			<view class="search-box">
				<picker mode="date" :value="searchDate" start="2000-01-01" end="2100-12-31" @change="onDateChange">
					<view class="picker">
						<text>{{ searchDate || '选择日期' }}</text>
					</view>
				</picker>
				<button class="search-btn" @tap="searchByDate">查询</button>
			</view>
			<view class="year-selector">
				<picker mode="date" fields="year" :value="currentYear" start="2000" end="2100" @change="onYearChange">
					<view class="picker">
						<text>{{ currentYear }}年</text>
					</view>
				</picker>
			</view>
		</view>

		<scroll-view class="solar-terms-list" scroll-y>
			<view v-if="loading" class="loading">
				<text>加载中...</text>
			</view>
			<view v-else-if="error" class="error">
				<text>{{ error }}</text>
			</view>
			<view v-else>
				<view v-for="(term, index) in solarTerms" :key="index" class="term-item" @tap="showTermDetail(term)">
					<view class="term-header">
						<text class="term-name">{{ term.name }}</text>
						<text class="term-date">{{ term.date }}</text>
					</view>
					<text class="term-meaning">{{ term.meaning }}</text>
				</view>
			</view>
		</scroll-view>

		<!-- 节气详情弹窗 -->
		<view v-if="showDetail" class="detail-modal">
			<view class="modal-content">
				<view class="modal-header">
					<text class="modal-title">{{ selectedTerm.name }}</text>
					<text class="close-btn" @tap="closeDetail">×</text>
				</view>
				<scroll-view class="modal-body" scroll-y>
					<view class="detail-item">
						<text class="detail-label">日期：</text>
						<text>{{ selectedTerm.date }}</text>
					</view>
					<view class="detail-item">
						<text class="detail-label">时间：</text>
						<text>{{ selectedTerm.time }}</text>
					</view>
					<view class="detail-item">
						<text class="detail-label">气候：</text>
						<text>{{ selectedTerm.climate }}</text>
					</view>
					<view class="detail-item">
						<text class="detail-label">习俗：</text>
						<text>{{ selectedTerm.xisu }}</text>
					</view>
					<view class="detail-item">
						<text class="detail-label">描述：</text>
						<text>{{ selectedTerm.desc }}</text>
					</view>
					<view class="detail-item">
						<text class="detail-label">谚语：</text>
						<view class="proverb-list">
							<text v-for="(proverb, index) in selectedTerm.proverb" :key="index" class="proverb-item">
								{{ proverb }}
							</text>
						</view>
					</view>
				</scroll-view>
			</view>
		</view>
	</view>
</template>

<script>
import { getSolarTermsByYear, getSolarTermByDate } from '../../api/solarTerm.js';

export default {
	data() {
		return {
			currentYear: new Date().getFullYear().toString(),
			searchDate: '',
			solarTerms: [],
			loading: false,
			error: '',
			showDetail: false,
			selectedTerm: null
		}
	},
	onLoad() {
		this.loadSolarTerms();
	},
	methods: {
		async loadSolarTerms() {
			this.loading = true;
			this.error = '';
			try {
				const data = await getSolarTermsByYear(this.currentYear);
				this.solarTerms = data;
			} catch (error) {
				this.error = error.message || '获取节气数据失败';
			} finally {
				this.loading = false;
			}
		},
		async searchByDate() {
			if (!this.searchDate) {
				uni.showToast({
					title: '请选择日期',
					icon: 'none'
				});
				return;
			}
			
			this.loading = true;
			this.error = '';
			try {
				const data = await getSolarTermByDate(this.searchDate);
				if (data.exists) {
					this.showTermDetail(data);
				} else {
					uni.showToast({
						title: '该日期不是节气日',
						icon: 'none'
					});
				}
			} catch (error) {
				uni.showToast({
					title: error.message || '查询失败',
					icon: 'none'
				});
			} finally {
				this.loading = false;
			}
		},
		onYearChange(e) {
			this.currentYear = e.detail.value;
			this.loadSolarTerms();
		},
		onDateChange(e) {
			this.searchDate = e.detail.value;
		},
		showTermDetail(term) {
			this.selectedTerm = term;
			this.showDetail = true;
		},
		closeDetail() {
			this.showDetail = false;
			this.selectedTerm = null;
		}
	}
}
</script>

<style>
.container {
	padding: 20rpx;
	background-color: #f5f5f5;
	height: 100vh;
}

.header {
	padding: 20rpx;
	background-color: #fff;
	border-radius: 10rpx;
	margin-bottom: 20rpx;
}

.title {
	font-size: 36rpx;
	font-weight: bold;
	color: #333;
	text-align: center;
	margin-bottom: 20rpx;
}

.search-box {
	display: flex;
	align-items: center;
	margin-bottom: 20rpx;
}

.picker {
	flex: 1;
	padding: 10rpx 20rpx;
	background-color: #f8f8f8;
	border-radius: 6rpx;
}

.search-btn {
	margin-left: 20rpx;
	padding: 0 30rpx;
	height: 60rpx;
	line-height: 60rpx;
	background-color: #007aff;
	color: #fff;
	border-radius: 6rpx;
	font-size: 28rpx;
}

.year-selector {
	text-align: center;
	padding: 10rpx 0;
}

.solar-terms-list {
	height: calc(100vh - 300rpx);
}

.term-item {
	background-color: #fff;
	padding: 20rpx;
	margin-bottom: 20rpx;
	border-radius: 10rpx;
}

.term-header {
	display: flex;
	justify-content: space-between;
	align-items: center;
	margin-bottom: 10rpx;
}

.term-name {
	font-size: 32rpx;
	font-weight: bold;
	color: #333;
}

.term-date {
	font-size: 28rpx;
	color: #666;
}

.term-meaning {
	font-size: 28rpx;
	color: #666;
}

.detail-modal {
	position: fixed;
	top: 0;
	left: 0;
	right: 0;
	bottom: 0;
	background-color: rgba(0, 0, 0, 0.5);
	z-index: 999;
	display: flex;
	justify-content: center;
	align-items: center;
}

.modal-content {
	width: 90%;
	max-height: 80vh;
	background-color: #fff;
	border-radius: 10rpx;
	overflow: hidden;
}

.modal-header {
	padding: 20rpx;
	display: flex;
	justify-content: space-between;
	align-items: center;
	border-bottom: 1rpx solid #eee;
}

.modal-title {
	font-size: 32rpx;
	font-weight: bold;
}

.close-btn {
	font-size: 40rpx;
	color: #999;
	padding: 0 20rpx;
}

.modal-body {
	padding: 20rpx;
	max-height: calc(80vh - 100rpx);
}

.detail-item {
	margin-bottom: 20rpx;
}

.detail-label {
	font-size: 28rpx;
	font-weight: bold;
	color: #666;
	margin-right: 10rpx;
}
.proverb-list {
	display: flex;
	flex-wrap: wrap;
}
.proverb-item {
	font-size: 28rpx;
	color: #666;
	margin-right: 10rpx;
	margin-bottom: 10rpx;
}
	
</style>